﻿#include "panda/log.h"
#include <chrono>
#include <iomanip>
#include <thread>
#include <xlnt/xlnt.hpp>

using namespace std;

template <typename Func, typename... Args>
static double timeFunction(Func f, Args&&... args)
{
	auto start = chrono::high_resolution_clock::now();
	forward<Func>(f)(forward<Args>(args)...);
	auto end = chrono::high_resolution_clock::now();

	chrono::duration<double> duration = end - start;
	cout << "Duration: " << duration.count() << " seconds\n";
	return duration.count();
}

static void log_test()
{
	LOG(log_level::debug, "This is a debug message.");
	LOG(log_level::info, "This is an informational message.");
	LOG(log_level::warning, "This is a warning message.");
	LOG(log_level::error, "This is an error message.");
	LOG_WARN("这真的是一个警告信息.");
}

static void delayed_log_test(int _millitime)
{
	this_thread::sleep_for(chrono::milliseconds(_millitime));
	LOG(log_level::info, "Delayed log after time");
}


// 读取Excel文件并转换为二维数组
vector<vector<string>> read_excel_to_2d_array(const string& filename, int sheet_index = 0) {
	vector<vector<string>> result;
	try {
		// 创建工作簿对象并加载Excel文件
		xlnt::workbook wb;
		wb.load(filename);
		// 获取指定工作表
		xlnt::worksheet ws = wb.sheet_by_index(sheet_index);
		cout << "正在读取工作表: " << ws.title() << "\n";
		// 遍历所有行
		for (const auto& row : ws.rows()) {
			vector<string> current_row;
			// 遍历当前行的所有单元格
			for (const auto& cell : row) {
				// 将单元格值转换为字符串并添加到当前行
				current_row.push_back(cell.to_string());
			}
			result.push_back(current_row);
		}
	}
	catch (const exception& e) {
		cerr << "读取Excel文件时出错: " << e.what() << "\n";
	}
	return result;
}

// 打印二维数组
void print_2d_array(const vector<vector<string>>& array) {

	if (array.empty()) {
		cout << "数组为空" << "\n";
		return;
	}
	cout << "\n读取到的二维数组内容 (" << array.size() << "行):" << "\n";

	for (size_t i = 0; i < array.size(); ++i) {
		cout << "第" << (setw(3)) << i + 1 << "行: [";
		for (size_t j = 0; j < array[i].size(); ++j) {
			cout << array[i][j];
			if (j < array[i].size() - 1) {
				cout << ", ";
			}
		}
		cout << "]\n";
	}


	size_t total_rows = array.size();
	size_t total_cells = 0;
	size_t max_cols = 0;

	for (const auto& row : array) {
		total_cells += row.size();
		if (row.size() > max_cols) {
			max_cols = row.size();
		}
	}

	cout << "\n数组统计信息:" << "\n";
	cout << "总行数: " << total_rows << "\n";
	cout << "总元素数量: " << total_cells << "\n";
	cout << "最大列数: " << max_cols << "\n";
	cout << "最小列数: " << (array.empty() ? 0 : array[0].size()) << "\n";
}


int main()
{

	//SetConsoleOutputCP(CP_UTF8);
	system("chcp 65001");
	// 测试基本日志功能
	log_test();

	// 测试时间测量功能
	timeFunction(delayed_log_test,2000);

	// 配置日志输出选项
	logger::get_instance().set_log_file("custom.log");
	logger::get_instance().enable_console(false);

	LOG(log_level::info, "This message only goes to file");

	// 恢复控制台输出
	logger::get_instance().enable_console(true);
	LOG(log_level::info, "Back to both console and file");
	string path = "D:/Mycpp/App/测试.xlsx";
	// getline(cin, path);c++

	vector<vector<string>> res = read_excel_to_2d_array(path);
	print_2d_array(res);
	//get_array_statistics(res);

	xlnt::workbook wb;
	xlnt::worksheet ws = wb.active_sheet();
	ws.cell("A1").value("Hello, Excel!");
	ws.cell("B1").formula("=A1");
	wb.save("example.xlsx");
	cout << "Excel file created successfully!\n";
	LOG(log_level::debug, "This is a debug message");
	system("pause");
	return 0;
}
